import {
  getTokenByCode
} from '/api/user'
import {
  getMchInfo,IPLocation,LBSLocation
} from '/api/amap'
import Event from '/utils/Event';
const util = require('/utils/util.js')
//app.js
App({
  event: new Event(),
  onLaunch: function (options) {
    var _this = this;
    _this.screenSize(); //获取可视窗口的尺寸
    wx.getSystemInfo({
      success: function (res) {
        _this.globalData.rpxR = 750 / res.windowWidth;
        _this.globalData.windowWidth = res.windowWidth * _this.globalData.rpxR;
        _this.globalData.windowHeight = res.windowHeight * _this.globalData.rpxR;
        _this.globalData.statusBarHeight = res.statusBarHeight * _this.globalData.rpxR;
      }
    })
    //获取经纬度
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy:true,
      success (res) {
        _this.globalData.lat = res.latitude
        _this.globalData.lng = res.longitude
        LBSLocation({
          lat:res.latitude,
          lng:res.longitude
        }).then(ress=>{
          _this.globalData.address = ress.data.address
          _this.globalData.areaId=ress.data.areaId
          _this.globalData.areaName=ress.data.areaName
        })
      },fail:err=>{
      }
    })
    // 检查是否登录
    wx.login({
      success: res => {
        console.log(res);
        // 发送 res.code 到后台换取 openId 并获取令牌
        getTokenByCode({
          mchId: _this.globalData.mchId,
          auth_code: res.code,
          uuid: _this.globalData.uuid,
          pluginId: 'weixinMiniAuthPlugin'
        }).then(
          res => {
            if (res.type == 'success') {
              const {
                userInfo,
                userId,
                token
              } = res.data
              _this.globalData.uuid = userId
              _this.globalData.token = token
              _this.globalData.userInfo = userInfo
            }
            if (_this.APP_TYPE > 0) {
              getMchInfo({ xuid: _this.globalData.xuid }).then(res => {
                if (res.data != null && res.data.sysMch != null) {
                  _this.globalData.mchId = res.data.sysMch.id
                  _this.globalData.mchName = res.data.sysMch.name
                  _this.globalData.location = res.data.locked;
                  if (res.data.shop != null) {
                    _this.globalData.shopId = res.data.shop.id
                    _this.globalData.shopName = res.data.shop.name
                  }
                }
                if (_this.userInfoReadyCallback) {
                  console.log('查看是否有调用')
                  _this.globalData.loaded = true;
                  _this.userInfoReadyCallback(res)
                }
              }).catch(error => {
                console.log(error)
              })
            } else {
              if (_this.userInfoReadyCallback) {
                _this.globalData.location = true;
                console.log('查看是否有调用2')
                _this.globalData.loaded = true;
                _this.userInfoReadyCallback(res)
              }
            }
          }
        ).catch(error => {
          console.log(error)
          console.log('4')
        })
      },
      fail: err => {
        if (_this.APP_TYPE > 0) {
          if (_this.userInfoReadyCallback) {
            console.log('查看是否有调用3')
            _this.globalData.loaded = true;
            _this.userInfoReadyCallback(res)
          }
          console.log('5')
        } else {
          if (_this.userInfoReadyCallback) {
            _this.globalData.loaded = true;
            _this.globalData.location = true;
            console.log('查看是否有调用4')
            _this.userInfoReadyCallback(res)
          }
        }
      }
    })
  },
  // APP_BASE_API: 'https://boot.rzico.com:8443',
  APP_BASE_API: 'https://boot.congcongkeji.cn:8443',
  APP_OSSURL_API: 'https://rzico.oss-cn-shenzhen.aliyuncs.com',
  // 0 单商户版 1.服务商版
  APP_TYPE: 0,
  VERSION: 0,
  ISV_ID: '10200',
  globalData: {
    mapKey: 'GSFBZ-J6VRU-YZFVZ-4SJWY-ZLARF-IHFYZ',  //葫芦派
    mchId: '10200', //葫芦派  wxeae1a099a54f95ca
    mchName: '莆仙餐巴',
    location: false,
    uuid: null,
    lat:'',
    lng:'',
    address:'',
    areaId:'',
    areaName:'',
    token: '',
    userInfo: { id: '' },
    xuid: null,
    shopId: null,
    shopName: null,
    loaded: false
  },
  //获取可视窗口宽度
  screenSize: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        //可视窗口宽度
        var ww = res.windowWidth;
        //可视窗口高度
        var hh = res.windowHeight;
        that.globalData.ww = ww;
        that.globalData.hh = hh;
        that.globalData.safeArea = res.safeArea;
      }
    })
  },
})
